package leetcode.link;

public class Test142 {

    //1,2,3,4,3,4,3,4,3,4,3,4,3,4
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode slow = head,fast = head;
        if (fast.next == null){
            return null;
        }
        while (fast.next != null){
            slow = slow.next;
            ListNode temp = fast.next;
            if (temp.next == null){
                return null;
            }
            fast = temp.next;
            //第一次相遇，表明已入环
            if (slow == fast){
                if (slow == head){
                    return head;
                }
                while (head != slow){
                    head = head.next;
                    slow = slow.next;
                }
                return head;
            }
        }
        return null;
    }
}
